var selectedDataRow = '';
var vueObj = new Vue({
    el: "#vue",
    data: {
        logObj: defLogInfo
    }
});
var isInit = true;

$(function () {
    window.actionEvents = {
        'click .infoView': function(e, value, row, index) {
            //编辑操作
            selectedDataRow = row;
            openQueryOrgPage();
        },
        'click .isvInfoView': function(e, value, row, index) {
            //编辑操作
            selectedDataRow = row;
            openCommonIsvPage(row.isvPid, 'queryOrg');
        },
    }
    initTextSearch("#isvName",
        basePath + 'checkOrg/getIsvData?t='+new Date().getTime()+'&isvName=',
        ['isvPid', 'isvName'],
        {'isvPid':'服务商PID', 'isvName':'服务商名称'},
        'isvPid',
        'isvName',
        true,
        selectIsvValue,
        noSelectIsvValue,
        false);
    /*laydate.render({
        elem: '#beginTimeStr'
        ,done: function(value, date, endDate){
            //控件选择完毕后的回调
            //点击日期、清空、现在、确定均会触发。回调返回三个参数，分别代表：生成的值、日期时间对象、结束的日期时间对象
            // setAge(value);
        }
    });*/
    laydate.render({
        elem: '#timeInterval'
        ,type: 'datetime'
        ,range: true //或 range: '~' 来自定义分割字符
        // ,value: dateFtt("yyyy-MM-dd hh:mm:ss", preDate) + ' - ' + dateFtt("yyyy-MM-dd hh:mm:ss", curDate)
        //js赋值重置失效，改为就是怕、页面赋值
        ,isInitValue: true //是否允许填充初始值，默认为 true
    });
});

function selectIsvValue(data) {
    if (!data || !data.isvPid) {
        return;
    }
    $('#isvPid').val(data.isvPid);
}

function noSelectIsvValue() {
    $('#isvPid').val('');
}

function doRefresh() {
    selectedDataRow = '';
    var result = $('#result').val();
    var orgName = $('#orgName').val();
    var checkType = $('#checkType').val();
    var provinceCode = $('#province').val();
    var cityCode = $('#city').val();
    var orgCategory = $('#orgCategory').val();
    var isvPid = $('#isvPid').val();
    // var beginTimeStr = $('#beginTimeStr').val();
    var gmtStart = '';
    var gmtEnd = '';
    var timeInterval = $("#timeInterval").val();
    if(timeInterval){
        var dates = timeInterval.split(' - ');
        if(dates && dates.length >1){
            gmtStart = dates[0];
            gmtEnd = dates[1];
        }
    }
    $('#dataTable').bootstrapTable('refresh', {
        url: basePath + 'checkOrg/pageFindQueryData?t=' + new Date().getTime(),
        query: {
            result : result,
            orgName : orgName,
            checkType : checkType,
            orgProvinceCode : provinceCode,
            orgCityCode : cityCode,
            orgCategory : orgCategory,
            isvPid : isvPid,
            beginTimeStr : gmtStart,
            endTimeStr : gmtEnd,
        },
    });
}

function getParams(params) {
    var result = $('#result').val();
    var orgName = $('#orgName').val();
    var checkType = $('#checkType').val();
    var provinceCode = $('#province').val();
    var cityCode = $('#city').val();
    var orgCategory = $('#orgCategory').val();
    var isvPid = $('#isvPid').val();
    // var beginTimeStr = $('#beginTimeStr').val();
    var pageSize = params.pageSize;
    var pageNo = params.pageNumber;
    if (isInit) {
        isInit = false;
        pageSize = defLogInfo.pageSize ? defLogInfo.pageSize : params.pageSize;
        pageNo = defLogInfo.pageNo ? defLogInfo.pageNo : params.pageNumber;
    }
    var gmtStart = '';
    var gmtEnd = '';
    var timeInterval = $("#timeInterval").val();
    if(timeInterval){
        var dates = timeInterval.split(' - ');
        if(dates && dates.length >1){
            gmtStart = dates[0];
            gmtEnd = dates[1];
        }
    }
    return {
        orgName : orgName,
        result : result,
        checkType : checkType,
        orgProvinceCode : provinceCode,
        orgCityCode : cityCode,
        orgCategory : orgCategory,
        isvPid : isvPid,
        beginTimeStr : gmtStart,
        endTimeStr : gmtEnd,
        pageSize : pageSize,
        pageNo : pageNo
    };
}

function doSelectCity() {
    var province = $('#province').val();
    if (!province || province == '') {
        $('#city').val('');
        var str = '<option value="">--请选择--</option>';
        $('#city').html(str);
        doRefresh();
    } else {
        province = province.substring(0, 2);
        var postData = {
            dictCode : province
        };
        $.ajax({
            type: 'post',
            url: basePath + 'checkOrg/selectCity?t=' + new Date().getTime(),
            dataType: 'json',
            data: postData,
            success : function (data) {
                if(data && data.result){
                    setCitySelect(data.content);
                }else if(data&&!data.result){
                    toastr.error(data.message);
                }else{
                    toastr.error('获取城市信息失败');
                }
            },
            error: function (request) {
                delayToastr(common_system_error);
            }
        });
    }
}

function setCitySelect(cityList) {
    $('#city').val('');
    var htmlStr = '<option value="">--请选择--</option>';
    if (cityList) {
        for (var i=0; i<cityList.length; i++) {
            htmlStr += '<option value="'+cityList[i].code+'">'+cityList[i].name+'</option>';
        }
    }
    $('#city').html(htmlStr);
    doRefresh();
}

function openQueryOrgPage() {
    if (!selectedDataRow || selectedDataRow == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    if (!selectedDataRow.id || selectedDataRow.id == '') {
        toastr.error('所选数据存在问题，请刷新页面重试');
        return;
    }
    var tableOptions = $('#dataTable').bootstrapTable('getOptions', null);
    window.location.href = basePath + 'checkOrg/checkOrgInfoPage?&id=' + selectedDataRow.id +
        '&type=query&t=' + new Date().getTime() + '&' + $('#searchForm').serialize() + '&isCheckExist=' + defLogInfo.isCheckExist +
        '&pageNo=' + tableOptions.pageNumber + '&pageSize=' + tableOptions.pageSize;
}

//------------------------------------------------------------------------列表数据初始化

function formatCheckType(value, row, index) {
    if (checkTypeList) {
        for (var i=0; i<checkTypeList.length; i++) {
            if (value == checkTypeList[i].key) {
                return checkTypeList[i].value;
            }
        }
    }
    return value;
}

function formatResult(value, row, index) {
    if (value == 'pass') {
        return '通过';
    }
    if (value == 'unpass') {
        return '未通过';
    }
    return value;
}

//------------------------------------------------------------------------列表选中事件
//上边选中事件
$("#dataTable").on("click-row.bs.table",function(e,row,ele){
    $("#dataTable .success").removeClass("success");
    $(ele).addClass("success");
    selectedDataRow = row;
});
